#include "jdialog.h"

#include <QVBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QFrame>
#include <QHBoxLayout>
#include <QLayout>
#include <QMouseEvent>
#include <QPoint>
#include <QDebug>
#include "../configargs.h"

#include <QApplication>
#include <qapplication.h>
#include <qeventloop.h>
#include <QEventLoop>

#include <qfile.h>
#include <QFile>


Folder2::Folder2(QWidget *parent,int i) : QDialog(parent)
{
    m_pos=0;
    count=0;
    QStringList filter;
   
    QPushButton * closeButton = new QPushButton();
    closeButton->setText(tr("选择"));
    pdirModel=new QDirModel();
    psplitter = new QSplitter();
    psplitter->setObjectName(QString::fromUtf8("splitter"));
    ptreeView = new QTreeView(psplitter);
    ptreeView->setObjectName(QString::fromUtf8("treeView"));

    plistView = new QListView(psplitter);
    plistView->setObjectName(QString::fromUtf8("listView"));
    plistView->setContextMenuPolicy(Qt::ActionsContextMenu);

    ptreeView->setBackgroundRole(QPalette::Dark);
    ptreeView->setAutoFillBackground(true);

    ptreeView->setModel(pdirModel);

 	ptreeView->setRootIndex(pdirModel->index("/"));
    ptreeView->setAlternatingRowColors(true);
    
    if(g_lcdsize == LCDSIZE_1024x768 || g_lcdsize == LCDSIZE_1280x800){
    	ptreeView->setIconSize( QSize(60, 60) );
    }else if(g_lcdsize == LCDSIZE_800x600){
        ptreeView->setIconSize( QSize(40, 40) );
    }else if(g_lcdsize == LCDSIZE_800x480){
        ptreeView->setIconSize( QSize(40, 40) );
    }else if(g_lcdsize == LCDSIZE_480x272){
        ptreeView->setIconSize( QSize(20, 20) );
    }
    plistView->setModel(pdirModel);
    plistView->setViewMode(QListView::IconMode);
    plistView->setMouseTracking(true);
    plistView->setRootIndex(pdirModel->index(QDir::currentPath()));

    if(g_lcdsize == LCDSIZE_1024x768 || g_lcdsize == LCDSIZE_1280x800){
        plistView->setIconSize( QSize(90, 90) );
    }else if(g_lcdsize == LCDSIZE_800x600){
        plistView->setIconSize( QSize(50, 50) );
    }else if(g_lcdsize == LCDSIZE_800x480){
        plistView->setIconSize( QSize(50, 50) );
    }else if(g_lcdsize == LCDSIZE_480x272){
        plistView->setIconSize( QSize(20, 20) );
    }
    plistView->setMouseTracking(true);
	plistView->setMovement(QListView::Static);

    ptreeView->resizeColumnToContents(0);

    selectionModel=ptreeView->selectionModel();
    connect(plistView,SIGNAL(clicked(QModelIndex)),this,SLOT(changePath(QModelIndex)));
    connect(selectionModel,SIGNAL(currentChanged(const QModelIndex&,const QModelIndex&)),this,SLOT(Selected(const QModelIndex&,const QModelIndex&)));
    connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));

    count=i;
    if(count==1)
        path="/music/";
    if(count==2)
        path="/photo/";
//    psplitter->setMinimumSize(700,400);


	psplitter->setStretchFactor(0,70);
	psplitter->setStretchFactor(1,30);

	if(g_lcdsize == LCDSIZE_480x272){
		psplitter->setFixedSize(300,120);
		
	}else{
		psplitter->setMinimumSize(600,300);  //在音乐播放等地方点击弹出来的文件选择框体大小
	    psplitter->move(0, 0);
	}


	



    QVBoxLayout *tmplayout = new QVBoxLayout();
    tmplayout->addWidget(psplitter);
    tmplayout ->addWidget(closeButton);
    this->setWindowTitle(tr("选择文件夹"));
    this->setLayout(tmplayout);



}

void Folder2::Selected(const QModelIndex& current,const QModelIndex& previous)
{
    if(current==previous)
        return;
    QString selectedPath=pdirModel->filePath(current);
    QDir dir(selectedPath);
    QStringList nameFilter;
//    pdirModel1=new QDirModel(nameFilter,QDir::AllEntries|QDir::NoDotAndDotDot,QDir::Name|QDir::DirsFirst|QDir::Type);
	pdirModel1=new QDirModel(nameFilter,QDir::AllEntries,QDir::Name|QDir::DirsFirst|QDir::Type);

    pdirModel1->setReadOnly(false);
    plistView->setModel(pdirModel1);
    plistView->setRootIndex(pdirModel1->index(selectedPath));
    path=selectedPath;

    QFileInfo *info=new QFileInfo(path);
    if(info->isDir())
    {
        int a,j=0;
        for(a=pathList.count()-m_pos;a>0;a--)
        {
            pathList.removeAt(m_pos+j);
        }
        this->pathList.append(selectedPath);
        m_pos++;
    }
    if(1==count){
	    QFile file1("/usrfs/qtapp/music.conf");
	    file1.open(QFile::WriteOnly);
	    QTextStream s(&file1);
	    s<<path;
	    file1.close();
	    qDebug()<< "selected:"<<path;
	    qDebug("1");
    }

    if(2==count){
        QFile file1("/usrfs/qtapp/photo.conf");
        file1.open(QFile::WriteOnly);
        QTextStream s(&file1);
        s<<path;
        file1.close();
        qDebug()<<path;
        qDebug("2");
    }

}

void Folder2::changePath(QModelIndex index)
{
    QFileInfo *info=new QFileInfo(pdirModel1->filePath(index));
    if(info->isDir())
    {
        this->pathList.append(pdirModel1->filePath(index));
        m_pos++;
        plistView->setRootIndex(index);
        ptreeView->setCurrentIndex(pdirModel->index(pdirModel1->filePath(index)));
    }
}

Folder2::~Folder2()
{
#ifdef QT_NO_QWS_CURSOR

	QApplication::setOverrideCursor(Qt::BlankCursor);
#endif
}

void JDialog::setLabel(QString type)
{
	QPixmap wifiimg(":/image/linuxdesktop/settingsicon/wifisignal4.png"); 
	QPixmap burnimg(":/image/linuxdesktop/settingsicon/cpuburn.png");  
	

	if(nowhavewifi == 1)
	{
		label_2->setPixmap(wifiimg);
	}else{
		label_2->clear();
	}

	if(cpuburn_flag == 1)
	{
		label_1->setPixmap(burnimg);
	}else{
		label_1->clear();
	}



	
	qDebug("nowhavewifi=%d cpuburn=%d", nowhavewifi, cpuburn_flag);



}


JDialog::JDialog(QWidget *parent)
    :QDialog(parent)
{
    QPushButton *pReBtn = new QPushButton();
    QPushButton *pMaxBtn = new QPushButton();
    m_pTitleLab = new QLabel(tr("Title"));

    label_1 = new QLabel(this);
	label_2 = new QLabel(this);
	
	label_1->setScaledContents(true);
	label_2->setScaledContents(true);
	
    DebugOrRelease=0;
    m_pTitleLayout = new QHBoxLayout();
    m_pTitleLayout->addWidget(m_pTitleLab);
    m_pTitleLayout->addStretch();
    m_pTitleLayout->addWidget(label_1);
    m_pTitleLayout->addWidget(label_2);
    m_pTitleLayout->addWidget(pMaxBtn);
    m_pTitleLayout->addWidget(pReBtn);
    m_pTitleLayout->setContentsMargins(15, 5, 15, 5);
    m_pTitleLayout->setSpacing(15);

    m_pTitleFrame = new QFrame();
    m_pTitleFrame->setLayout(m_pTitleLayout);
    m_pTitleFrame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);

    m_pMainLayout = new QVBoxLayout();
    m_pMainLayout->addWidget(m_pTitleFrame, 0, Qt::AlignTop);
    m_pMainLayout->setMargin(0);
    m_pMainLayout->setSpacing(0);

    m_pTitleFrame->setObjectName("jDialogTitleFrame");
    pReBtn->setObjectName("jDialogReturnBtn");
    pMaxBtn->setObjectName("jDialogMaxBtn");
    m_pTitleLab->setObjectName("jDialogTitleLab");
    label_1->setObjectName("jDialogLabel_1");
	label_2->setObjectName("jDialogLabel_2");

    m_pTitleLab->setFocusPolicy(Qt::NoFocus);
    pReBtn->setFocusPolicy(Qt::NoFocus);
    pMaxBtn->setFocusPolicy(Qt::NoFocus);

	setLabel(QString("auto"));

    this->setLayout(m_pMainLayout);

    connect(pReBtn, SIGNAL(clicked(bool)), this, SLOT(close()));
    connect(pMaxBtn, SIGNAL(clicked(bool)), this, SLOT(slotMaxBtnClicked()));

    this->setWindowFlags(Qt::FramelessWindowHint);
    if (DebugOrRelease)
    this->setCursor(Qt::CrossCursor);
    m_pPressPoint = new QPoint(this->width(), this->height());
    m_isTopPoint = false;
}

JDialog::~JDialog()
{
#ifdef QT_NO_QWS_CURSOR

	QApplication::setOverrideCursor(Qt::BlankCursor);
#endif
}

void JDialog::setJTitle(QString text)
{
    m_pTitleLab->setText(text);
}

void JDialog::setJLayout(QLayout *pLayout)
{
    m_pSubLayout = pLayout;
    m_pSubLayout->setContentsMargins(SUBLEFTMARGIN, SUBTOPMARGIN, SUBRIGHTMARGIN, SUBBOTTOMMARGIN);
    m_pMainLayout->addLayout(m_pSubLayout, 1);
}

void JDialog::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton) {
        if(event->pos().y() < XBEGIN) {
            m_isTopPoint = true;
            m_pPressPoint = new QPoint(event->pos());
            return;
        }
        else {
            m_isTopPoint = false;
        }
    }
    QWidget::mousePressEvent(event);
}

void JDialog::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton && m_isTopPoint) {
        int xp = m_pPressPoint->x() - event->x();
        int yp = m_pPressPoint->y() - event->y();
        if((xp < XOFFSET && xp > -XOFFSET) && (yp > YOFFSET || yp < -YOFFSET)) {
            m_pSubLayout->setContentsMargins(40, 15, 40, 15);
            m_pMainLayout->insertWidget(0, m_pTitleFrame);
            m_pTitleFrame->setVisible(true);
            return;
        }
    }
    QWidget::mouseReleaseEvent(event);
}

void JDialog::slotMaxBtnClicked()
{
    m_pSubLayout->setMargin(15);
    m_pMainLayout->removeItem(m_pMainLayout->itemAt(0));
    m_pTitleFrame->setVisible(false);
}
void JDialog::keyBoardInput(QString text)
{
    QPushButton *pSaveButton= new QPushButton(tr("A"));
    QGridLayout *pSubLayout = new QGridLayout;
    pSubLayout->addWidget(pSaveButton,			0, 0, 1, 3);
    this->setLayout(pSubLayout);
}
void JDialog::slotcked()
{
    m_pSubLayout->setMargin(15);
    m_pMainLayout->removeItem(m_pMainLayout->itemAt(0));
 //   m_pTitleFrame->setVisible(false);
}
